﻿// ***********************************************************************
// Assembly         : SecurityLayer
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="ChangePasswordController.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.SecurityLayer.Models.AccountModels.MembershipServices;
using InnovaApp.Layers.SecurityLayer.Models.ViewModels._LayoutViewModels;
using INNOVACALL.Framework.Common;

namespace InnovaApp.Layers.SecurityLayer.Controllers.ViewControllers.LayoutViewControllers
{
    /// <summary>
    /// Class ChangePasswordController
    /// </summary>
    public class ChangePasswordController : _LayoutViewControllers_Base<InnovaApp.Layers.SecurityLayer.Models.ViewModels._LayoutViewModels.ChangePassword_ViewModel>
    {
        /// <summary>
        /// Gets or sets the forms service.
        /// </summary>
        /// <value>The forms service.</value>
        public IFormsAuthenticationService FormsService { get; set; }
        /// <summary>
        /// Gets or sets the membership service.
        /// </summary>
        /// <value>The membership service.</value>
        public IMembershipService MembershipService { get; set; }

        /// <summary>
        /// Initializes data that might not be available when the constructor is called.
        /// </summary>
        /// <param name="requestContext">The HTTP context and route data.</param>
        protected override void Initialize(RequestContext requestContext)
        {
            base.eMethodBase = "InnovaApp.Layers.SecurityLayer.Controllers.ChangePasswordController";
            base.specificCssCollection = new string[] { "Views/Account/AccountCommon.css", "ClientComponents/KendoUI/kendo." + InnovaApp.Layers.CommonLayer.Controllers.Environment.KendoUITheme + ".min.css" };
            base.specificJSCollection = null;
            base.dynamicJSCollection = new string[] { "/ScriptsG/ChangePasswordDynamic.js" };
            base.specificJqueryJSCollection = new string[] { "Views/ChangePassword.js" };
            base.externalCss = null;
            base.Meta.Add(enMeta.Description, Resources.View_ChangePassword_Meta_Description);
            base.Meta.Add(enMeta.Keywords, Resources.View_ChangePassword_OrderWithDetails_Meta_Keywords);
            base.Meta.Add(enMeta.Subject, Resources.View_ChangePassword_OrderWithDetails_Meta_Subject);

            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }
        /// <summary>
        /// URL[Get]: /ChangePassword
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Authorize, HttpGet]
        public ActionResult Index()
		{
            //NEVER USES VIEW BAG AGAIN !
            using (DataRepositories _dataContext = new DataRepositories())
			{
                base.InitView(
                    pageTitle: Resources.View_Register_PageTitle,
                    openingMessage: string.Empty,
                    specificCssCollection: specificCssCollection,
                    specificJSCollection: specificJSCollection,
                    dynamicJSCollection: dynamicJSCollection,
                    specificJqueryJSCollection: specificJqueryJSCollection,
                    jsVariables: jsVariables,
                    externalJs: externalJs,
                    externalCss: externalCss,
                    metaToAdd: Meta,
                    _dataContext: _dataContext,
                    displayMenus: false,
                    language: base.RequestLanguage,
                    adminMode: false,
                    usesKendoUI: true,
                    jQueryUITheme: null,
                    kendoUITheme: null,
                    currentUser: Membership.GetUser()
                    );
			}

			return View(ContentData);
		}

        /// <summary>
        /// URL[Post]: /ChangePassword
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>JsonResult.</returns>
        [Authorize, HttpPost]
        public JsonResult Index(ChangePassword_ViewModel model)
        {
            JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            MembershipUser user = Membership.GetUser();
            if (MembershipService.ChangePassword(user.UserName, model.PasswordFields.OldPassword, model.PasswordFields.NewPassword))
            {
                toReturn.Data = new { @success = true, @messageTitle = Resources.View_ChangePassword_Password_Changed_Successfully_MessageTitle, @message = Resources.View_ChangePassword_Password_Changed_Successfully };
            }
            else
            {
                toReturn.Data = new { @success = false, @message = Resources.View_ChangePassword_Failed };
            }
            return toReturn;
        }
    }
}
